<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud ref="crud" :page="page" :data="tableData" :permission="permissionList" :table-loading="tableLoading"
                 :option="tableOption" @on-load="getList" @search-change="searchChange" @refresh-change="refreshChange"
                 @size-change="sizeChange" @current-change="currentChange"
                 @row-update="handleUpdate" @row-save="handleSave" @row-del="rowDel">
        <template slot="menuLeft" slot-scope="scope">
          <el-radio-group v-model="radioState" @change="stateChange">
            <el-radio-button label="全部"></el-radio-button>
            <el-radio-button label="未审核"></el-radio-button>
            <el-radio-button label="已通过"></el-radio-button>
            <el-radio-button label="已拒绝"></el-radio-button>
          </el-radio-group>
        </template>
        <template slot="menu" slot-scope="scope">
          <el-button type="text" icon="el-icon-check" @click="handlePass(scope.row)">通 过</el-button>
          <el-button type="text" icon="el-icon-close" @click="handleRefuse(scope.row)">拒 绝</el-button>
          <el-button type="text" icon="el-icon-view" @click="handleDetail(scope.row)">查 看</el-button>
        </template>
      </avue-crud>
    </basic-container>

    <el-dialog title="查 看" :visible.sync="dialogDetailVisible">
      <div class="container" v-html="contentHtml" >{{contentHtml}}</div>
    </el-dialog>
  </div>
</template>

<script>
import {fetchList, getObj, addObj, putObj, delObj, getContent} from '@/api/creation/article'
import {tableOption} from '@/const/crud/creation/article'
import {mapGetters} from 'vuex'

export default {
  name: 'article',
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      radioState: '0',
      dialogDetailVisible: false,
      contentHtml: ''
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.creation_article_add, true),
        delBtn: this.vaildData(this.permissions.creation_article_del, true),
        editBtn: this.vaildData(this.permissions.creation_article_edit, false)
      };
    }
  },
  created() {
    this.radioState = "全部";
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    rowDel: function (row, index) {
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange(current) {
      this.page.currentPage = current
    },
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    refreshChange() {
      this.getList(this.page)
    },
    stateChange(label) {
      let value = label === '全部' ? '' : this.$refs.crud.DIC['stateFlag'].find(k => k.label === label).value;
      let form = {
        stateFlag: value
      }
      this.getList(this.page, Object.assign(form, this.searchForm));
    },
    handlePass(row) {
      this.$confirm('是否确认通过文章标题为' + row.title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        putObj(Object.assign({
          id: row.id,
          stateFlag: '1'
        }))
      }).then(data => {
        this.$message.success('操作成功')
        this.getList(this.page)
      }).catch(() => {
        this.$message.error('操作失败')
      })
    },
    handleRefuse(row) {
      this.$confirm('是否确认拒绝文章标题为' + row.title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        putObj(Object.assign({
          id: row.id,
          stateFlag: '2'
        }))
      }).then(data => {
        this.$message.success('操作成功')
        this.getList(this.page)
      }).catch(() => {
        this.$message.error('操作失败')
      })
    },
    handleDetail(row) {
      getContent(row.id).then(response => {
        if (response.data.code === 0) {
          this.dialogDetailVisible = true;
          this.contentHtml = response.data.data;
        } else {
          this.$message.error("无法查询文章详情！")
        }
        console.log("response => ", response);
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.avue-crud .avue-crud__left .el-button {
  margin-right: -1px;
}
</style>
